package com.gitee.wsl.struct.dict

import com.gitee.wsl.data.store.StoreServer

object DictionaryExt {

    val dictMap = mutableMapOf<String, DictionaryMap<*, *>>()

    fun <T : Any,V:Any> getDictionary(state: StoreServer, dictName:String): DictionaryMap<T, V> {
        dictMap[dictName]?.let {
            return it as DictionaryMap<T, V>
        }
        val defValue= DictionaryMapImp<T, V>(dictName)
        val dict = state.getPreferenceOrDefault(dictName,defValue)
        dictMap[dictName]=dict
        return dict
    }

    fun isExits(state: StoreServer, dictName:String):Boolean{
        return state.isExits(dictName)
    }

    fun <T : Any,V:Any> flushDictionary(state: StoreServer, dict: DictionaryMap<T, V>){
        state.putPreference(dict.name,dict)
    }

    fun flushDictionary(state: StoreServer, dictName:String){
        dictMap[dictName]?.let {
            state.putPreference(dictName,it)
        }
    }

}